/**
 * 
 */
package cn.com.xf.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import cn.com.xf.domain.*;
import cn.com.xf.service.ChinaareaService;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.xf.common.AbstractBaseController;
import cn.com.xf.common.Constants;
import cn.com.xf.common.StringUtil;
import cn.com.xf.service.AreaService;
import cn.com.xf.utils.encode.EncrypTool;
import cn.com.xf.web.PageModel;
import cn.com.xf.web.XFMappingJacksonJsonView;
import cn.com.xf.web.form.AreaForm;
import cn.com.xf.web.form.AreaMapForm;
import cn.com.xf.web.form.DestinForm;

/**
 * @author Administrator
 * 
 */
@Controller
@RequestMapping("/area")
public class AreaController extends AbstractBaseController {
	@Autowired
	private AreaService areaService;
	@Autowired
	private ChinaareaService chinaareaService;

	@RequestMapping
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response, AreaForm form) throws Exception {
		form.setTab(Constants.TAB_AREA);
		ModelAndView mv = new ModelAndView("/area/index", Constants.FORM_COMMAND_KEY, form);
		return mv;
	}

	/**
	 * 已经存在的区域列表
	 * 
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/list")
	public ModelAndView area_list(HttpServletRequest request, HttpServletResponse response, AreaForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/area/area_list", Constants.FORM_COMMAND_KEY, form);
		// 1.模糊搜索关键字
		String siteId = StringUtil.StringFilter(form.getSiteId());
		String siteName = StringUtil.StringFilter(form.getSiteName());
		String markId = StringUtil.StringFilter(form.getMarkId());
		String markName = StringUtil.StringFilter(form.getMarkName());
		String areaId = StringUtil.StringFilter(form.getAreaId());
		String areaName = StringUtil.StringFilter(form.getAreaName());

		form.setSiteId(siteId);
		form.setSiteName(siteName);
		form.setMarkId(markId);
		form.setMarkName(markName);
		form.setAreaId(areaId);
		form.setAreaName(areaName);

		// 2.总记录数
		int count = areaService.selectBatDstareaCount(siteId, siteName, markId, markName, areaId, areaName);
		if (count == 0) {
			return mv;
		}

		// 3.分页信息
		int pageNum = 1;
		if (StringUtils.isNotBlank(form.getPageNum())) {
			pageNum = Integer.valueOf(form.getPageNum());
		}
		PageModel pageModel = new PageModel(count, PageModel.DEFAULT_PAGE_SIZE_AREA, pageNum, form);
		int begin = pageModel.getBegin();
		int pageSize = pageModel.getPageSize();

		// 4.排序规则
		String orderByField = "siteid";
		String orderByMode = "desc";
		if (StringUtils.isNotBlank(form.getOrderByField()) && StringUtils.isNotBlank(form.getOrderByMode())) {
			if ("fldidd".equalsIgnoreCase(form.getOrderByField())) {
				orderByField = "fldidd";
			}
			if ("fldsitenames".equalsIgnoreCase(form.getOrderByField())) {
				orderByField = "fldsitenames";
			}
			if ("siteName".equalsIgnoreCase(form.getOrderByField())) {
				orderByField = "siteName";
			}
			if ("areaName".equalsIgnoreCase(form.getOrderByField())) {
				orderByField = "areaName";
			}
			if ("asc".equalsIgnoreCase(form.getOrderByMode()) || "desc".equalsIgnoreCase(form.getOrderByMode())) {
				orderByMode = form.getOrderByMode();
			}
		}

		// 5.查询
		List<BatDstarea> list = areaService.selectBatDstarea(begin, pageSize, orderByField, orderByMode, siteId,
				siteName, markId, markName, areaId, areaName);
		mv.addObject("list", list);
		return mv;
	}

	/**
	 * 编辑页面
	 * 
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/edit")
	public ModelAndView editNotice(HttpServletRequest request, HttpServletResponse response, AreaForm form)
			throws Exception {
		String fldiddStr = form.getFldidd();
		String realFldidd = "";
		if (StringUtils.isNotBlank(fldiddStr)) {
			realFldidd = EncrypTool.dencode(fldiddStr);
		}
		ModelAndView mv = new ModelAndView("/area/edit", Constants.FORM_COMMAND_KEY, form);
		VDstarea varea = areaService.selectVDstareaByFldidd(realFldidd);
		mv.addObject("varea", varea);
		return mv;
	}

	/**
	 * 编辑区域执行
	 * 
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/doEdit")
	public ModelAndView doEditNotice(HttpServletRequest request, HttpServletResponse response, @Valid AreaForm form,
			BindingResult result) throws Exception {
		ModelAndView mv = new ModelAndView("/area/edit_result", Constants.FORM_COMMAND_KEY, form);
		List<ObjectError> errorList = null;
		if (result.hasErrors()) {
			errorList = result.getAllErrors();
			// 将错误信息Json化
			String errorJson = errorToJson(errorList);
			mv.addObject("errorJson", errorJson);
			return mv;
		}
		//检测所属抬头是否是手动输入(即没有通过下拉框选择)并自动校正
		String areaId = form.getAreaId();
		Boolean isNumber = areaId.matches("^\\d+$");
		if(!isNumber){
			String res = areaService.selectAreaIdByAreaName(areaId);
			if(res!=null&&res.matches("^\\d+$")){
				form.setAreaId(res);
				String ofSite = areaService.selectOfSiteByAreaname(areaId);
				form.setSiteId(ofSite);
			}else{
				mv.addObject("status", "FAILED");
				mv.addObject("msg", "所属抬头无效！");
				return mv;
			}
		}
		areaService.updateBatDstarea(form);
		mv.addObject("status", "SUCCESS");
		return mv;
	}

	/**
	 * 增加区域
	 * 
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/add")
	public ModelAndView addArea(HttpServletRequest request, HttpServletResponse response, AreaForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/area/add", Constants.FORM_COMMAND_KEY, form);
		return mv;
	}

	/**
	 * 增加区域执行
	 * 
	 * @param request
	 * @param response
	 * @param form
	 * @param result
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/doAdd")
	public ModelAndView doAdd(HttpServletRequest request, HttpServletResponse response, @Valid AreaForm form,
			BindingResult result) throws Exception {
		ModelAndView mv = new ModelAndView("/area/add_result", Constants.FORM_COMMAND_KEY, form);
		List<ObjectError> errorList = null;
		if (result.hasErrors()) {
			errorList = result.getAllErrors();
			// 将错误信息Json化
			String errorJson = errorToJson(errorList);
			mv.addObject("errorJson", errorJson);
			return mv;
		}
		// 检测区域是否存在
		int count = areaService.selectBatDstareaCountByOfAreaId(form.getAreaId().trim());
		if (count > 0) {
			mv.addObject("status", "FAILD");
			mv.addObject("msg", "抬头【" + form.getAreaId().trim() + "】已经建立区域");
			return mv;
		}
		areaService.insertBatDstarea(form);
		mv.addObject("status", "SUCCESS");
		return mv;
	}

	/**
	 * 抬头列表
	 * 
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/areaList")
	public void areaList(HttpServletRequest request, HttpServletResponse response, AreaForm form) throws Exception {
		String areaName = StringUtil.StringFilter(form.getQ());
		List<Tbareainfo> areaList = areaService.selectTbareainfoListByAreaName(areaName);
		List<Map> list = new ArrayList<Map>();
		for (Tbareainfo area : areaList) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("id", area.getAreaid());
			map.put("text", area.getAreaname());
			list.add(map);
		}
		String jsonString = JSONArray.toJSONString(list);
		response.getWriter().write(jsonString);
	}

	@RequestMapping(value = "/doDeleteArea")
	public ModelAndView doDeleteArea(HttpServletRequest request, HttpServletResponse response, AreaForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/area/add_result", Constants.FORM_COMMAND_KEY, form);
		String areaIdStr = form.getAreaId();
		String areaIdReal = EncrypTool.dencode(areaIdStr);
		String fldiddStr = form.getFldidd();
		String fldiddReal = EncrypTool.dencode(fldiddStr);
		areaService.deleteArea(areaIdReal, fldiddReal);
		mv.addObject("status", "SUCCESS");
		return mv;
	}

	/**
	 * 根据抬头查询站点
	 * 
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/querySite")
	public ModelAndView querySite(HttpServletRequest request, HttpServletResponse response, AreaForm form)
			throws Exception {
		String areaId = form.getAreaId();

		// 5.查询
		Tbareainfo tbareaInfo = areaService.selectAreainfoByAreaId(areaId);
		Map<String, String> map = new HashMap<String, String>();
		map.put("siteId", tbareaInfo.getOfsite());
		map.put("siteName", tbareaInfo.getSiteName());
		String jsonString = JSONObject.toJSONString(map);
		response.getWriter().write(jsonString);
		return null;
	}

	/**
	 * 编辑坐标
	 * 
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/editCoordinate")
	public ModelAndView editCoordinate(HttpServletRequest request, HttpServletResponse response, AreaForm form)
			throws Exception {
		String areaIdStr = form.getAreaId();
		String realAreaId = "";
		if (StringUtils.isNotBlank(areaIdStr)) {
			realAreaId = EncrypTool.dencode(areaIdStr);
		}
		form.setAreaName(StringUtil.StringFilter(form.getAreaName()));
		ModelAndView mv = new ModelAndView("/area/edit_coordinate", Constants.FORM_COMMAND_KEY, form);
		TblOaAreaCoordinate coordinate = areaService.selectTblOaAreaCoordinateByAreaId(realAreaId);
		mv.addObject("tblOaAreaCoordinate", coordinate);
		return mv;
	}

	/**
	 * 编辑坐标弹出层
	 * 
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/editCoordinatePopup")
	public ModelAndView editCoordinatePopup(HttpServletRequest request, HttpServletResponse response, AreaForm form)
			throws Exception {
		String areaIdStr = form.getAreaId();
		String realAreaId = "";
		if (StringUtils.isNotBlank(areaIdStr)) {
			realAreaId = EncrypTool.dencode(areaIdStr);
		}
		form.setAreaName(StringUtil.StringFilter(form.getAreaName()));
		ModelAndView mv = new ModelAndView("/area/edit_coordinate_popup", Constants.FORM_COMMAND_KEY, form);
		TblOaAreaCoordinate coordinate = areaService.selectTblOaAreaCoordinateByAreaId(realAreaId);
		mv.addObject("tblOaAreaCoordinate", coordinate);
		return mv;
	}

	/**
	 * 编辑坐标执行
	 * 
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/doEditCoordinate")
	public ModelAndView doEditCoordinate(HttpServletRequest request, HttpServletResponse response, AreaForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/area/edit_coordinate_result", Constants.FORM_COMMAND_KEY, form);
		String areaIdStr = form.getAreaId();
		String realAreaId = "";
		if (StringUtils.isNotBlank(areaIdStr)) {
			realAreaId = EncrypTool.dencode(areaIdStr);
		}
		String coordinate = form.getCoordinate();
		if (StringUtils.isNotBlank(coordinate)) {
			String coordinateStr = form.getCoordinate();
			String x = coordinateStr.split(",")[0];
			String y = coordinateStr.split(",")[1];

			areaService.insertOrUpdateAreaCoordinate(realAreaId, x, y);
			mv.addObject("status", "SUCCESS");
			mv.addObject("msg", "修改成功");
		} else {
			mv.addObject("status", "FALID");
			mv.addObject("msg", "请正确选择坐标");
		}
		return mv;
	}
	/**
	 * 围栏地图
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/listMap")
	public ModelAndView map_list(HttpServletRequest request, HttpServletResponse response, AreaMapForm form)
			throws Exception {
		ModelAndView mv = null;
		String siteid = StringUtil.StringFilter(form.getSiteid());
	
		if(siteid.equals("")){
			mv = new ModelAndView("/area/map_allList", Constants.FORM_COMMAND_KEY, form);
		}else{
			mv = new ModelAndView("/area/map_list", Constants.FORM_COMMAND_KEY, form);
		}
		
		// 1.模糊搜索关键字
		
		String areaname = StringUtil.StringFilter(form.getAreaname());
		String province = StringUtil.StringFilter(form.getProvince());
		String city = StringUtil.StringFilter(form.getCity());
		form.setAreaname(areaname);

		// 2.总记录数
		int count = areaService.selectMapCount(siteid, areaname,province,city);
		if (count == 0) {
			return mv;
		}

		// 3.分页信息
		int pageNum = 1;
		if (StringUtils.isNotBlank(form.getPageNum())) {
			pageNum = Integer.valueOf(form.getPageNum());
		}
		PageModel pageModel = new PageModel(count, PageModel.DEFAULT_PAGE_SIZE_AREA, pageNum, form);
		int begin = pageModel.getBegin();
		int pageSize = pageModel.getPageSize();

		// 4.排序规则
		String orderByField = "ENTERDATE";
		String orderByMode = "desc";
		if (StringUtils.isNotBlank(form.getOrderByField()) && StringUtils.isNotBlank(form.getOrderByMode())) {
			
			if ("siteid".equalsIgnoreCase(form.getOrderByField())) {
				orderByField = "siteid";
			}
			if ("areaname".equalsIgnoreCase(form.getOrderByField())) {
				orderByField = "areaname";
			}
			if ("asc".equalsIgnoreCase(form.getOrderByMode()) || "desc".equalsIgnoreCase(form.getOrderByMode())) {
				orderByMode = form.getOrderByMode();
			}
		}

		// 5.查询
		List<TbAppAreas> list = areaService.selectMap(begin, pageSize, orderByField, orderByMode, siteid, areaname,province,city);
		mv.addObject("list", list);
		return mv;
	}
	
	@RequestMapping(value = "/addMap")
	public ModelAndView addMap(HttpServletRequest request, HttpServletResponse response, AreaMapForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/area/add_map", Constants.FORM_COMMAND_KEY, form);		
		return mv;
	}
	@RequestMapping(value = "/doAddMap")
	public ModelAndView doAddMap(HttpServletRequest request, HttpServletResponse response, @Valid AreaMapForm form,
			BindingResult result) throws Exception {
		ModelAndView mv = new ModelAndView("/area/add_map_result", Constants.FORM_COMMAND_KEY, form); 
		List<ObjectError> errorList = null;
		if (result.hasErrors()) {
			errorList = result.getAllErrors();
			// 将错误信息Json化
			String errorJson = errorToJson(errorList);
			mv.addObject("errorJson", errorJson);
			return mv;
		}
		int count = areaService.selectMapCount(form.getSiteid(), form.getAreaname(),"","");
		if(count >0){
			mv.addObject("status","FAILD");
			mv.addObject("msg","此围栏地图已添加！");
			return mv;
		}
		int resu = areaService.insertMap(form);
		if(resu == 1){
			mv.addObject("status","SUCCESS");
		}else{
			mv.addObject("status","FAILD");
			mv.addObject("msg","没有此围栏！");
		}
				
		return mv;
	}
	@RequestMapping(value = "/editMapSite")
	public ModelAndView editMapSite(HttpServletRequest request, HttpServletResponse response, AreaMapForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/area/edit_map_site", Constants.FORM_COMMAND_KEY, form);
		TbAppAreas areaMap = areaService.selectByMapid(form.getAreaid());
		List<TbAppAreas> areaMapList=areaService.selectMapByCity(areaMap.getCity(),areaMap.getAreaid());
		mv.addObject("areaMapList", areaMapList);
		mv.addObject("areaMap", areaMap);
		Tbchinaarea tbchinaarea=chinaareaService.selectCityNameByAreaId(areaMap.getCity());
		String city=null;
		if (null!=tbchinaarea){
			city=tbchinaarea.getCity();
		}
		List<TbAppAreas> xy = areaService.selectLngLat(areaMap.getCity());
		List<Map> listxy = new ArrayList<>();
		for (TbAppAreas tbAppAreas : xy) {
			Map<String,String> map = new HashMap<>();
			map.put("pointname", tbAppAreas.getAreaname());
			map.put("lng", tbAppAreas.getLng());
			map.put("lat", tbAppAreas.getLat());
			listxy.add(map);
		}
		String coordinate = JSONArray.toJSONString(listxy);
		mv.addObject("coordinate", coordinate);
		mv.addObject("city", city);
		return mv;
	}
	@RequestMapping(value = "/editMapCore")
	public ModelAndView editMapCore(HttpServletRequest request, HttpServletResponse response, AreaMapForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/area/edit_map_core", Constants.FORM_COMMAND_KEY, form);
		TbAppAreas areaMap = areaService.selectByMapid(form.getAreaid());
		List<TbAppAreas> areaMapList=areaService.selectMapByCity(areaMap.getCity(),areaMap.getAreaid());
		mv.addObject("areaMapList", areaMapList);
		mv.addObject("areaMap", areaMap);
		Tbchinaarea tbchinaarea=chinaareaService.selectCityNameByAreaId(areaMap.getCity());
		String city=null;
		if (null!=tbchinaarea){
			city=tbchinaarea.getCity();
		}
		List<TbAppAreas> xy = areaService.selectLngLat(areaMap.getCity());
		List<Map> listxy = new ArrayList<>();
		for (TbAppAreas tbAppAreas : xy) {
			Map<String,String> map = new HashMap<>();
			map.put("pointname", tbAppAreas.getAreaname());
			map.put("lng", tbAppAreas.getLng());
			map.put("lat", tbAppAreas.getLat());
			listxy.add(map);
		}
		String coordinate = JSONArray.toJSONString(listxy);
		mv.addObject("coordinate", coordinate);
		mv.addObject("city", city);
		return mv;
	}
	@RequestMapping(value = "/checkSiteMap")
	public ModelAndView checkSiteMap(HttpServletRequest request, HttpServletResponse response, AreaMapForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/area/check_map_site", Constants.FORM_COMMAND_KEY, form);
		TbAppAuditingAreas areaMap = areaService.selectByAuditingMapid(form.getAreaid());
		List<TbAppAreas> areaMapList=areaService.selectMapByCity(areaMap.getCity(),areaMap.getAreaid());
		mv.addObject("areaMapList", areaMapList);
		mv.addObject("areaMap", areaMap);
		Tbchinaarea tbchinaarea=chinaareaService.selectCityNameByAreaId(areaMap.getCity());
		String city=null;
		if (null!=tbchinaarea){
			city=tbchinaarea.getCity();
		}
		List<TbAppAreas> xy = areaService.selectLngLat(areaMap.getCity());
		List<Map> listxy = new ArrayList<>();
		for (TbAppAreas tbAppAreas : xy) {
			Map<String,String> map = new HashMap<>();
			map.put("pointname", tbAppAreas.getAreaname());
			map.put("lng", tbAppAreas.getLng());
			map.put("lat", tbAppAreas.getLat());
			listxy.add(map);
		}
		String coordinate = JSONArray.toJSONString(listxy);
		mv.addObject("coordinate", coordinate);
		mv.addObject("city", city);
		return mv;
	}
	@RequestMapping(value = "/auditingAreaMap")
	public ModelAndView auditingAreaMap(HttpServletRequest request, HttpServletResponse response,AreaMapForm form)
			throws Exception {
		String siteid = StringUtil.StringFilter(form.getSiteid());
		ModelAndView mv = new ModelAndView("/area/map_auditingList",Constants.FORM_COMMAND_KEY,form);
				// 1.模糊搜索关键字
				String areaname = StringUtil.StringFilter(form.getAreaname());
				form.setAreaname(areaname);
				// 2.总记录数
				int count = areaService.selectMapCountAuditing(siteid, areaname);
				if (count == 0) {
					return mv;
				}
				// 3.分页信息
				int pageNum = 1;
				if (StringUtils.isNotBlank(form.getPageNum())) {
					pageNum = Integer.valueOf(form.getPageNum());
				}
				PageModel pageModel = new PageModel(count, PageModel.DEFAULT_PAGE_SIZE_AREA, pageNum, form);
				int begin = pageModel.getBegin();
				int pageSize = pageModel.getPageSize();

				// 4.排序规则
				String orderByField = "ENTERDATE";
				String orderByMode = "desc";
				if (StringUtils.isNotBlank(form.getOrderByField()) && StringUtils.isNotBlank(form.getOrderByMode())) {
					
					if ("siteid".equalsIgnoreCase(form.getOrderByField())) {
						orderByField = "siteid";
					}
					if ("areaname".equalsIgnoreCase(form.getOrderByField())) {
						orderByField = "areaname";
					}
					if ("asc".equalsIgnoreCase(form.getOrderByMode()) || "desc".equalsIgnoreCase(form.getOrderByMode())) {
						orderByMode = form.getOrderByMode();
					}
				}
				// 5.查询
				List<TbAppAuditingAreas> list = areaService.selectMapAuditing(begin, pageSize, orderByField, orderByMode, siteid, areaname);
			
				mv.addObject("list", list);
				return mv;
	}
	@RequestMapping(value = "/doEditCoreMap")
	public ModelAndView doEditCoreMap(HttpServletRequest request, HttpServletResponse response, @Valid AreaMapForm form,
			BindingResult result) throws Exception {
		ModelAndView mv = new ModelAndView("/area/edit_map_result", Constants.FORM_COMMAND_KEY, form);
		List<ObjectError> errorList = null;
		if (result.hasErrors()) {
			errorList = result.getAllErrors();
			// 将错误信息Json化
			String errorJson = errorToJson(errorList);
			mv.addObject("errorJson", errorJson);
			return mv;
		}		
		//操作人员权限判断，中心人员修改执行updateMap();
		//分部人员修改执行auditingMap();到临时auditing表，等待审核
		//审核完毕，覆盖合并到正式表
		/*int resu = areaService.auditingMap(form);*/
		int resu = areaService.updateMap(form);
		String judge = form.getJudge();
		if(resu == 1){
			mv.addObject("status","SUCCESS");
			mv.addObject("judge",judge);
		}else{
			mv.addObject("status","FAILD");
		}	
		return mv;
	}
	@RequestMapping(value = "/doEditMap")
	public ModelAndView doEditMap(HttpServletRequest request, HttpServletResponse response, @Valid AreaMapForm form,
			BindingResult result) throws Exception {
		ModelAndView mv = new ModelAndView("/area/edit_map_result", Constants.FORM_COMMAND_KEY, form);
		List<ObjectError> errorList = null;
		if (result.hasErrors()) {
			errorList = result.getAllErrors();
			// 将错误信息Json化
			String errorJson = errorToJson(errorList);
			mv.addObject("errorJson", errorJson);
			return mv;
		}		
		/**
		 * 判断该地图是否已经修改过
		 * ischecked = 1
		 * int resu = areaService.updateAuditing(form);
		 **/
		int resu = -1;
		if(null != areaService.selectByAuditingMapid(form.getAreaid())){
			resu = areaService.updateAuditing(form);
		}else{
			resu = areaService.auditingMap(form);
		}
		String judge = form.getJudge();
		if(resu == 1){
			areaService.checkMap_0(form.getAreaid());
			mv.addObject("status","SUCCESS");
			mv.addObject("judge",judge);
		}else{
			mv.addObject("status","FAILD");
		}	
		return mv;
	}
	
	@RequestMapping(value = "/doAuditingMap")
	public ModelAndView doAuditingMap(HttpServletRequest request, HttpServletResponse response, @Valid AreaMapForm form,
			BindingResult result) throws Exception {
		ModelAndView mv = new ModelAndView("/area/edit_map_result", Constants.FORM_COMMAND_KEY, form);
		TbAppAuditingAreas areaMap = areaService.selectByAuditingMapid(form.getAreaid());
		form.setPoints(areaMap.getPoints());
		//获取当前时间，存入测试表
		//form.setCheckdate(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
		//更新测试表审核时间
		areaService.updateCheckDate(form);
		List<ObjectError> errorList = null;
		if (result.hasErrors()) {
			errorList = result.getAllErrors();
			// 将错误信息Json化
			String errorJson = errorToJson(errorList);
			mv.addObject("errorJson", errorJson);
			return mv;
		}		
		int resu = areaService.updateMap(form);
		if(resu == 1){
			//set ISCHECKED = 1 变更审核判定字段——已审核
			areaService.checkMap_1(form.getAreaid());
			mv.addObject("status","SUCCESS");
		}else{
			mv.addObject("status","FAILD");
		}	
		return mv;
	}
	
	@RequestMapping(value = "/doDelMap")
	public ModelAndView doDelMap(HttpServletRequest request, HttpServletResponse response, AreaMapForm form)
			throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		int result = areaService.delMap(form.getAreaid());
		if (result==1) {
			map.put("status", "SUCCESS");
		} else{
			map.put("status", "FAILD");
		}
		ModelAndView mv = new ModelAndView(new XFMappingJacksonJsonView(), map);
		return mv;
	}
	@RequestMapping(value = "/delAuditingMap")
	public ModelAndView delAuditingMap(HttpServletRequest request, HttpServletResponse response, AreaMapForm form)
			throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		int result = areaService.delAuditingMap(form.getAreaid());
		
		if (result==1) {
			//ISDELETED = 1 未执行删除
			map.put("status", "SUCCESS");
		} else{
			map.put("status", "FAILD");
		}
		ModelAndView mv = new ModelAndView(new XFMappingJacksonJsonView(), map);
		return mv;
	}
	
	@RequestMapping(value = "/getArea")
	@ResponseBody
	public void getReg(HttpServletRequest request, HttpServletResponse response, String areaname,String siteid) throws Exception{
		Map<String,String> map = new HashMap<String, String>();
		map.put("areaname", areaname);
		if(siteid ==null){
			map.put("siteid", "");
		}else{
			map.put("siteid", siteid);
		}
		List<BatDstarea> list= areaService.selectByAreaname(map);
		List<Map<String,String>> listmap = new ArrayList<Map<String,String>>();
		if(list != null && list.size() > 0){
			for(BatDstarea bat:list){
				String areaName = bat.getFldsitenames();
				String sites = bat.getSiteId();
				String sitename = bat.getSiteName();
				String prov = bat.getProvince();
				String city = bat.getCity();
				Map<String,String> resultMap = new HashMap<String, String>();
				resultMap.put("label","抬头:"+areaName + " | 站点:" + sitename + " | ID:" + sites);
				resultMap.put("areaname", areaName);
				resultMap.put("siteid",sites);
				resultMap.put("sitename", sitename);
				resultMap.put("prov", prov);
				resultMap.put("city", city);
				listmap.add(resultMap);
			}
		}
		Map<String, Object> returnMap = new HashMap<String, Object>();
		returnMap.put("datas", listmap);
		String mapStr = JSONObject.toJSONString(returnMap);
		response.setContentType("text/html;charset=UTF-8");   
	    response.getWriter().write(mapStr);
	
	}
}
